package com.qcby.service.impl;

import com.qcby.dao.MovieMapper;
import com.qcby.model.Movie;
import com.qcby.model.dto.MovieActorVo;
import com.qcby.model.dto.MovieConsoleVo;
import com.qcby.model.dto.MovieShowVo;
import com.qcby.query.MovieQuery;
import com.qcby.service.MovieService;
import com.qcby.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

@Service
public class MovieServiceImpl extends BaseServiceImpl<MovieQuery, Movie> implements MovieService {


    private MovieMapper movieDao;

    @Autowired
    public void setBrandDao(MovieMapper movieDao) {
        this.movieDao = movieDao;
        this.baseDao = movieDao;
    }

    @Override
    public Integer getMid(String mName) {
        return movieDao.getMid(mName);
    }

    @Override
    public Page<MovieShowVo> getByMName(String mName, Integer pageNo, Integer pageSize) {
        Integer startNum = (pageNo - 1) * pageSize;
        Page<MovieShowVo> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setStartNum(startNum);
        List<MovieShowVo> list = movieDao.getByMName(mName, startNum, pageSize);
        for (MovieShowVo movieShowVo : list) {
            movieShowVo.setAvgMovieScore(movieShowVo.getAvgMovieScore().setScale(2, RoundingMode.HALF_UP));
        }
        Integer count = movieDao.getByMNameCount(mName);
        page.setTotalCount(count);
        page.setList(list);
        page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
        return page;
    }

    @Override
    public MovieShowVo getCommentByMid(Integer mid) {
        return movieDao.getCommentByMid(mid);
    }

    @Override
    public List<MovieActorVo> getActorByMid(Integer mid) {
        return movieDao.getActorByMid(mid);
    }

    @Override
    public Page<MovieShowVo> selectByCondition(MovieQuery mq, String condition) {
        Integer pageNo = mq.getPageNo();
        Integer pageSize = mq.getPageSize();
        Integer startNum = (pageNo - 1) * pageSize;
        Page<MovieShowVo> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setStartNum(startNum);


        String movieYear = mq.getMovieYear();
        Integer mtId = mq.getMtId();
        String orderBy;
        List<MovieShowVo> movieShowVoList = new ArrayList<>();
        Integer count = -1;
        if ("0".equals(condition)) {
            orderBy = "count_paid_order";
            movieShowVoList = movieDao.selectOrderByOrder(mtId, movieYear, orderBy, startNum, pageSize);
            count = movieDao.selectOrderByOrderCount(mtId, movieYear, orderBy);

        } else if ("1".equals(condition)) {
            orderBy = "sum_box_office";
            movieShowVoList = movieDao.selectOrderByOrder(mtId, movieYear, orderBy, startNum, pageSize);
            count = movieDao.selectOrderByOrderCount(mtId, movieYear, orderBy);
        } else if ("2".equals(condition)) {
            movieShowVoList = movieDao.selectOrderByScore(mtId, movieYear, startNum, pageSize);
            count = movieDao.selectOrderByScoreCount(mtId, movieYear);
        } else {
            throw new IllegalArgumentException("不合法的排序！");
        }

        //把总记录数设置给page对象
        page.setTotalCount(count);
        page.setList(movieShowVoList);
        page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
        return page;
    }

    @Override
    public void takeOffMovie(Integer mid) {
        movieDao.takeOffMovie(mid);
    }

    @Override
    public Page<MovieConsoleVo> query(Integer mtId, String mName, String mStatus, Integer pageNo, Integer pageSize) {
        Integer startNum = (pageNo - 1) * pageSize;
        Page<MovieConsoleVo> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setStartNum(startNum);

        List<MovieConsoleVo> list = movieDao.query(mtId, mName, mStatus, startNum, pageSize);
        for (MovieConsoleVo movieConsoleVo : list) {
            movieConsoleVo.getMovieActorVoList().addAll(movieDao.getActorByMid(movieConsoleVo.getMid()));
        }
        Integer count = movieDao.queryCount(mtId, mName, mStatus);

        page.setTotalCount(count);
        page.setList(list);
        page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
        return page;
    }
}
